﻿ //Tao mot ket noitoi FMS//
myConnection = new NetConnection ();
myConnection.connect (_root.CFG.fmsApp);

//Tao mot stream moi de nhan luong video/audio//
in_ns = new NetStream (myConnection);
in_ns.play (_global.nameStream);
//Tao mot stream moi de nhan luong du lieu//
get_ns = new NetStream (myConnection);
get_ns.play ("sendData_"+_global.nameStream);

//Cac bien//
var my_video : Video;
var mySound = new Sound;
var myLevel = 50;
var isactive = 0;
var flag:Boolean = false;
var oldDate = clock.strDate;
var labelStr:String = "Pause";
var label_txt;



//Cac doi tuong Component//

var motion_pb : mx.controls.ProgressBar;



////////////////////////////////////////////
/////////Khoi tao gia tri dau///////////////
////////////////////////////////////////////

my_video.attachVideo (in_ns);

//component ProgressBar
motion_pb.mode = "manual";
motion_pb.label = "Motion level %3%%";



//////////////////////////////////////////////
////////        Cac ham          /////////////
//////////////////////////////////////////////


//Nhan gia tri cua cac bien duoc truyen qua sendData,
//gan cho myLevel
get_ns.activeLevel = function (msg)
{
	myLevel = msg;
};

 //va gan cho isactive
get_ns.onActive = function (val)
{
	isactive = val;
};


//Doi mau cua ProgressBar khi my_cam.onActivity thay doi va Bao dong!!!
BaoDong = function (mystatus :Boolean)
{
	if (mystatus == true)
	{
		motion_pb.setStyle ("themeColor", "haloGreen");
		warn_snd.gotoAndStop (2);
		if (_root.flag == false)
		{
			//_root.mySound.attachSound ("DANGER");
			_root.mySound.loadSound (_root.CFG.soundFile, true);
			//_root.mySound.start();
			_root.flag = true;
		}
	} 
	else
	{
		motion_pb.setStyle ("themeColor", "haloOrange");
		warn_snd.gotoAndPlay (1);
		_root.flag = false;
	}
};


//Play Pause button
play_btn.onRelease = function ()
{
	if (_root.label_txt.text == "Play")
	{
		_root.label_txt.text = "Pause";
	}
	else
	{
		_root.mySound.stop ();
		_root.label_txt.text = "Play";
	}
	in_ns.pause ();
	get_ns.pause ();
};

//Back button
back_btn.onRelease = function()
{		
		_root.mySound.stop();
		my_video.attachVideo (null);
		my_video.clear ();
		in_ns.play (false);
		in_ns.close ();
		get_ns.close ();
		rsoMessages.connect (false);
		myConnection.close ();
		gotoAndStop ("strm3");
};


//Exit button
exit_btn.onRelease = function()
{		
		my_video.attachVideo (null);
		my_video.clear ();
		in_ns.play (false);
		in_ns.close ();
		get_ns.play (false);
		get_ns.close ();
		rsoMessages.connect (false);
		myConnection.close ();
		gotoAndPlay ("main");
};


//Restart lai ket noi (khi chuyen ngay)
restart = function ()
{
	my_video.attachVideo (null);
	in_ns.close ();
	get_ns.close();
	in_ns = new NetStream (myConnection);
	//in_ns.play (clock.strDate);
	in_ns.play (_root.nameStream);
	get_ns.play ("sendData_"+_root.nameStream);
	my_video.attachVideo (in_ns);
	oldDate = clock.strDate;
};

//////////

//OnEnterFrame
this.onEnterFrame = function ()
{
	motion_pb.setProgress (myLevel, 100);
	BaoDong (isactive);
	if (oldDate != clock.strDate)
	{
		//restart ();//Restart lai ket noi (khi chuyen ngay)
	}
};

label_txt.text = "Pause";
